<template>
  <div class="change-class body-bg">
    <div v-if="!initLoading">
      <ul class="change-content" v-if="list.length">
        <li class="package-items" v-for="(item,index) in list" :key="index">
          <p class="title">{{item.package_name}}</p>
          <div class="sign-items" v-for="(iItem,iIndex) in item.sub_goods" :key="iIndex" @click="goDet(iItem.goods_id,iItem.open_date,iItem.end_date)">
            <div class="item-cont">
              <img class="feng-mian" :src="iItem.teacher_photo || '/static/images/user.png'" alt="fengmian">
              <div class="item-cont-c">
                <p class="class-name overflow-one">{{iItem.goods_name}}</p>
                <p class="class-infor"><img src="/static/images/singUp/singUp-icon1.png" alt="icon">{{iItem.campus_name}}</p>
                <p class="class-infor"><img src="/static/images/singUp/singUp-icon2.png" alt="icon">{{iItem.open_date}} 至 {{iItem.end_date}}</p>
                <p class="class-infor"><img src="/static/images/singUp/singUp-icon3.png" alt="icon">{{iItem.goods_week}}</p>
              </div>
              <!-- <img class="taocan" src="/static/images/singUp/singUp-icon4.png" alt="套餐"> -->
            </div>
            <div class="items-tips">
              <p class="class-tip">
                <span class="items-tip-biaoqie" v-if="iItem.is_transfer == '1'">随时插班</span>
                <span class="items-tip-biaoqie" v-if="iItem.retreat_rule == '1'">随时退班</span>
                <span class="items-tip-biaoqie" v-if="iItem.is_audition == '1'">免费试听</span>
              </p>
              <div>
                <p class="discount-price">￥{{iItem.mall_price}}</p>
                <p class="price" v-if="iItem.mall_cost*1>iItem.mall_price*1">￥{{iItem.mall_cost}}</p>
              </div>
            </div>
          </div>
          <div class="bottom">
            <van-submit-bar
              label="总价："
              :suffix-label="discountText + item.discount_money"
              decimal-length="0"
              :price="item.payment_price*100"
              button-text="立即报名"
              @submit="onSubmit(index,item.package_id,item.package_name,item.mall_cost,item.mall_price,item.discount_money,item.payment_price)"
            />
          </div>
        </li>
        <li v-if="isReachBottom" class="pull-loading"><van-loading type="spinner" size="20px" color="#1989fa" /></li>
        <li v-if="isBottom" class="bottom-line">-- 亲，已经到最后了--</li>
      </ul>
      <div v-else class="empty">
        <img src="/static/images/empty-icon.png" alt="空">
        <p class="empty-tips">暂无数据</p>
      </div>
    </div>
    <div v-else class="init-loading"><van-loading type="spinner" color="#1989fa" /></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      from: "", // 记录路由来源 1 报名 2 续费
      initLoading: true, // 初始加载
      isBottom: false, // 是否加载完全部数据
      isReachBottom: false, // 是否显示加载状态
      goods_id: "", // 课程id
      renew_goods_id: "", // 续费班级id
      curr_page: 1,
      page_size: 10,
      total: "",
      discountText: "已优惠：￥",
      // 套餐list
      list: [],
    }
  },
  onLoad(e) {
    // url中传递了from参数，1 来自报名 2 来自续费 3 套餐
    this.from = e.from;
    this.goods_id = e.goods_id;
    this.renew_goods_id = e.renew_goods_id || '';
  },
  onShow() {
    this.getGoodsPackagesList();
  },
  onUnload() {
    this.initLoading = true;
  },
  // 下拉刷新
  onPullDownRefresh: function() {
    this.curr_page = 1;
    this.isReachBottom = false;
    this.isBottom = false;
    this.getGoodsPackagesList();
  },
  // 上拉加载
  onReachBottom () {
    if(this.isBottom) return;
    this.isReachBottom = true;
    this.getGoodsPackagesList();
  },
  methods: {
    // 获取列表
    getGoodsPackagesList() {
      wx.showNavigationBarLoading() //在标题栏中显示加载
      if(this.isReachBottom) {
        this.curr_page++;
      }
      this.$fetch.getGoodsPackagesList({
          goods_id: this.goods_id,
          curr_page: this.curr_page,
          page_size: this.page_size
      }).then(res=>{
        wx.hideNavigationBarLoading() //在标题栏中隐藏加载
        wx.stopPullDownRefresh(); //完成停止加载
        this.initLoading = false;
        this.total = res.result.total;
        let len = 0;
        if(this.isReachBottom) { // 判断是下拉还是上拉动作
          this.list = [...this.list, ...res.result.data];
          len = this.list.length;
          if(len >= this.total){
            this.isBottom = true;
          }
          this.isReachBottom = false;
          return
        }
        this.list = res.result.data;
        len = this.list.length;
        if(len >= this.total){
          this.isBottom = true;
        }
      }).catch(err=>{
        this.initLoading = false;
        this.isReachBottom = false;
        wx.hideNavigationBarLoading() //在标题栏中隐藏加载
        wx.stopPullDownRefresh(); //完成停止加载
      })
    },
    // 查看课程详情
    goDet(goods_id,open_date,end_date) {
      let that = this;
      wx.navigateTo({
        url: `/pages/signUpDet/main?from=3&goods_id=${goods_id}&open_date=${open_date}&end_date=${end_date}&renew_goods_id=${that.renew_goods_id}`
      })
    },
    // 去报名
    onSubmit(index,goods_id,package_name,mall_cost,mall_price,discount_money,payment_price) {
      let that = this;
      // 缓存套餐信息，用于生成订单
      this.$store.commit("setPackageList",this.list[index]);
      wx.navigateTo({
        url: `/pages/packageOrder/main?from=${that.from}&goods_id=${goods_id}&package_name=${package_name}&mall_cost=${mall_cost}&mall_price=${mall_price}&discount_money=${discount_money}&payment_price=${payment_price}&renew_goods_id${that.renew_goods_id}`
      })
    }
  }
}
</script>

<style lang="less">
.bottom{
  .van-submit-bar{
    position: relative !important;
  }
}
</style>
<style lang="less" scope>
.change-class{
  width: 100%;
  min-height: 100vh;
  overflow: hidden;
  .change-content{
    width: 100%;
    margin-top: 10px;
    .package-items{
      width: 100%;
      background: #fff;
      margin-bottom: 10px;
      .bottom{
        border-top: 0.5px solid #dcdcdc;
      }
      .title{
        width: 100%;
        height: 60px;
        line-height: 60px;
        padding: 0 18px;
        font-size: 16px;
        color: #000;
        font-weight: bold;
      }
      .sign-items{
        width: 100%;
        background: #fff;
        overflow: hidden;
        .item-cont{
          width: 100%;
          background: #E5F9FF;
          padding: 14px 22px;
          box-sizing: border-box;
          position: relative;
          display: flex;
          align-items: center;
          .feng-mian{
            min-width: 75px;
            width: 75px;
            height: 80px;
            display: block;
            margin-right: 12px;
          }
          .item-cont-c{
            width: 100%;
            overflow: hidden;
          }
          .taocan{
            position: absolute;
            top: 6px;
            right: 12px;
            width: 24px;
            height: 24px;
            display: block;
          }
          .class-name{
            width: 100%;
            font-size: 14px;
            color: #666;
            font-weight: bold;
            margin-bottom: 10px;
          }
          .class-infor{
            width: 100%;
            font-size: 12px;
            color: #999;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            margin-top: 5px;
            img{
              width: 12px;
              height: 12px;
              display: block;
              margin-right: 6px;
            }
          }
        }
        .items-tips{
          width: 100%;
          padding: 10px 12px;
          box-sizing: border-box;
          display: flex;
          justify-content: space-between;
          align-items: center;
          .class-tip{
            &.items-tips{
              width: 100%;
              box-sizing: border-box;
              display: flex;
              align-items: center;
              .items-tip-biaoqie{
                padding: 2px 8px;
                border: 1px solid rgba(0,183,238,1);
                border-radius: 8px;
                font-size: 12px;
                color: #00B7EE;
                margin-right: 12px;
              }
            }
          }
          .items-tip-biaoqie{
            padding: 2px 8px;
            border: 1px solid rgba(0,183,238,1);
            border-radius: 8px;
            font-size: 12px;
            color: #00B7EE;
            margin-left: 12px;
          }
          .price{
            text-decoration:line-through;
            color: #A9A9A9;
            font-size: 14px;
            font-weight: bold;
          }
          .discount-price{
            color: #EE0000;
            font-size: 14px;
            font-weight: bold;
          }
        }
      }
    }
  }
}
</style>
